#include "SelectPropertyWidget.h"

SelectPropertyWidget::SelectPropertyWidget(QWidget *parent) : QWidget(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);
    setWindowFlag(Qt::WindowStaysOnTopHint);

    deleteBtn = new QPushButton;
    deleteBtn->setMinimumSize(QSize(12,12));
    deleteBtn->setMaximumSize(QSize(12,12));
    deleteBtn->setStyleSheet("border-image: url(:/Polygon/Resource/deletePloygon.png);");
    connect(deleteBtn,&QAbstractButton::pressed,this,[&](){emit deleteBtnSlot(propertyNumber);hide();});

    hbLayout = new QHBoxLayout;
    hbLayout->addWidget(deleteBtn);
    hbLayout->setMargin(2);
    this->setLayout(hbLayout);
}

SelectPropertyWidget::~SelectPropertyWidget()
{
    if(hbLayout)
        delete hbLayout;
    hbLayout = nullptr;

    if(deleteBtn)
        delete deleteBtn;
    deleteBtn = nullptr;
}

void SelectPropertyWidget::setProperty(const QPair<QPoint,QPoint> &pos, const int &pn)
{
    int x1 = pos.first.x();
    int y1 = pos.first.y();
    int x2 = pos.second.x();
    int y2 = pos.second.y();
    int x3 = 0;
    int y3 = 0;

    // 判断该辅助矩形的形成方式
    if(x1<x2 && y1<y2)
    {
        x3 = x2;
        y3 = y2;
    }
    else if(x2<x1 && y1<y2)
    {
        x3 = x1;
        y3 = y2;
    }
    else if(x1<x2 && y2<y1)
    {
        x3 = x2;
        y3 = y1;
    }
    else if(x2<x1 && y2<y1)
    {
        x3 = x1;
        y3 = y1;
    }
    move(x3-5,y3+8);

    propertyNumber = pn;

    show();
}
